from celery import shared_task
from aaa import models
import random
from selenium import webdriver
from time import sleep

@shared_task
def modify_mgstate(xx):
    import pyecharts.options as opts
    from pyecharts.charts import Line
    from pyecharts.faker import Faker
    from drf2.f import offline_rate

    # x = [
    # 	['益阳', '益阳普适型地灾监测预警项目', '385', '353', '32', '91.69'],
    # 	['湘西', '湘西州地质灾害隐患普适监测项目', '295', '288', '7', '97.63'],
    # 	['长沙', '长沙市普适型地质灾害监测项目', '110', '107', '3', '97.27'],
    # 	['邵阳', '邵阳市普适型地灾监测预警项目', '329', '307', '22', '93.31'],
    # 	['张家界', '张家界市普适型地灾监测预警项目（2021年第二批）', '436', '429', '7', '98.39'],
    # 	['株洲', '株洲市普适型监测项目', '195', '173', '22', '88.72'],
    # 	['常德', '常德普适型测试项目', '495', '471', '24', '95.15'],
    # 	['娄底', '娄底市普适化地灾监测项目', '85', '78', '7', '91.76']
    # ]

    x = offline_rate()
    print('x', x)

    offline = []
    online = []
    total = []
    name = []
    for item in x:
        name.append(item[0])
        total.append(item[2])
        offline.append(item[-2])
        online.append(item[-1])
        c = (
            Line()
                .add_xaxis(name)
                .add_yaxis("设备在线率(%省略)", online, is_smooth=True)
                .add_yaxis("设备总数", total, is_smooth=True)
                .add_yaxis("掉线设备", offline, is_smooth=True)
                .set_global_opts(title_opts=opts.TitleOpts(title="1170项目云平台设备在线率"))
                .render("C:\drf2\drf2\line1_smooth.html")
        )
        # c.render("line1_smooth.html")

    # stat()

# @shared_task
# def modify_mgstate(xx):
#     print(333)
#     from lxml import etree
#     chromedriver = "C:\\Users\\0717\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"
#     bro = webdriver.Chrome(executable_path='C:\drf2\drf2\chromedriver.exe')
#     # element = driver.find_element_by_xpath("//input[@class='form-control' and @name='username']")
#     bro.get('http://localhost/')
#     # 定义隐式等待
#     bro.implicitly_wait(10)
#     # 标签定位
#     btn = bro.find_element_by_class_name('el-button')
#     btn.click()
#     x = bro.find_element_by_xpath('//ul[1]/li[3]')
#     x.click()
#     c = bro.find_element_by_xpath('//ul[1]/li[3]//li[2]')
#     c.click()
#     sleep(24)
#     bro.save_screenshot('x5{0}.png'.format(random.randint(100,200)))
#     sleep(3)
#     bro.quit()
